package com.ykz2015.chapter3.main;

import com.ykz2015.chapter3.config.Appconfig;
import com.ykz2015.chapter3.pojo.User;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class IoCTest {
    public static void main(String[] args) {

        //java配置文件传递参数给ioc容器的创建参数，这个配置文件就像ioc容器描述，我要装配什么bean，因为appconfig加了@bean，代表方法的返回会被加入到ioc容器中
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Appconfig.class);

        //通过名称【名称是唯一的】获取,假设只有一个user的情况下，需要使用(User)强制转换一下cast
        User user1 = (User) ctx.getBean("user");
        User user2 = (User) ctx.getBean("user");
        System.out.println(user1 == user2);//此处测试的是bean的作用域范围

        //通过类型获取，不需要强制cast，但是通过类型获取，如果有两个user类（比如user.java中存在user2），错误如下

        //        User user=ctx.getBean(User.class);
        /*
          18:38:17.378 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'user2'
          Exception in thread "main" org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.ykz2015.chapter3.pojo.User' available: expected single matching bean but found 2: user,user2
          	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveNamedBean(DefaultListableBeanFactory.java:1180)
          	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveBean(DefaultListableBeanFactory.java:416)
          	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:349)
          	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:342)
          	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1126)
          	at com.ykz2015.chapter3.main.IoCTest.main(IoCTest.java:18)

          Process finished with exit code 1
         */

        //如果又想按照类型，然后类型又有不同的实体，那么可以用“根据名称和类型获取bean”,以下仍然可以正常输出。
        User user = ctx.getBean("user", User.class);


        System.out.println(user.getUserName());//输出如下,看是否需要ctx.close()关闭。

        /*
          "C:\Program Files\Java\jdk1.8.0_181\bin\java.exe" "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2019.2.4\lib\idea_rt.jar=55349:D:\Program Files\JetBrains\IntelliJ IDEA 2019.2.4\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_181\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\rt.jar;E:\chapter3\target\classes;E:\apache-maven-3.6.0\maven-jar\repository\org\springframework\boot\spring-boot-starter\2.2.2.RELEASE\spring-boot-starter-2.2.2.RELEASE.jar;E:\apache-maven-3.6.0\maven-jar\repository\org\springframework\boot\spring-boot\2.2.2.RELEASE\spring-boot-2.2.2.RELEASE.jar;E:\apache-maven-3.6.0\maven-jar\repository\org\springframework\spring-context\5.2.2.RELEASE\spring-context-5.2.2.RELEASE.jar;E:\apache-maven-3.6.0\maven-jar\repository\org\springframework\spring-aop\5.2.2.RELEASE\spring-aop-5.2.2.RELEASE.jar;E:\apache-maven-3.6.0\maven-jar\repository\org\springframework\spring-beans\5.2.2.RELEASE\spring-beans-5.2.2.RELEASE.jar;E:\apache-maven-3.6.0\maven-jar\repository\org\springframework\spring-expression\5.2.2.RELEASE\spring-expression-5.2.2.RELEASE.jar;E:\apache-maven-3.6.0\maven-jar\repository\org\springframework\boot\spring-boot-autoconfigure\2.2.2.RELEASE\spring-boot-autoconfigure-2.2.2.RELEASE.jar;E:\apache-maven-3.6.0\maven-jar\repository\org\springframework\boot\spring-boot-starter-logging\2.2.2.RELEASE\spring-boot-starter-logging-2.2.2.RELEASE.jar;E:\apache-maven-3.6.0\maven-jar\repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;E:\apache-maven-3.6.0\maven-jar\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;E:\apache-maven-3.6.0\maven-jar\repository\org\apache\logging\log4j\log4j-to-slf4j\2.12.1\log4j-to-slf4j-2.12.1.jar;E:\apache-maven-3.6.0\maven-jar\repository\org\apache\logging\log4j\log4j-api\2.12.1\log4j-api-2.12.1.jar;E:\apache-maven-3.6.0\maven-jar\repository\org\slf4j\jul-to-slf4j\1.7.29\jul-to-slf4j-1.7.29.jar;E:\apache-maven-3.6.0\maven-jar\repository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;E:\apache-maven-3.6.0\maven-jar\repository\org\springframework\spring-core\5.2.2.RELEASE\spring-core-5.2.2.RELEASE.jar;E:\apache-maven-3.6.0\maven-jar\repository\org\springframework\spring-jcl\5.2.2.RELEASE\spring-jcl-5.2.2.RELEASE.jar;E:\apache-maven-3.6.0\maven-jar\repository\org\yaml\snakeyaml\1.25\snakeyaml-1.25.jar;E:\apache-maven-3.6.0\maven-jar\repository\org\slf4j\slf4j-api\1.7.29\slf4j-api-1.7.29.jar" com.ykz2015.chapter3.main.IoCTest
          18:31:37.706 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@5d22bbb7
          18:31:37.719 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
          18:31:37.809 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor'
          18:31:37.810 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
          18:31:37.812 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
          18:31:37.813 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
          18:31:37.818 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'appconfig'
          18:31:37.822 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'user'
          user_name_1

          Process finished with exit code 0
         */


    }
}
